package org.tity.test.java.cglib;

import java.lang.reflect.Method;

import org.tity.commons.lang.ArrayUtil;
import org.tity.sql.entity.Entity;

import com.alibaba.fastjson.JSONArray;

import junit.framework.Assert;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.ImmutableBean;
import net.sf.cglib.proxy.CallbackHelper;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.FixedValue;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.NoOp;

public class SampleClass {
	
	
	public String test(String txt){
		txt="hello world:"+txt;
        System.out.println(txt);
        return txt;
    }

    public static void main(String[] args) throws Exception {
//        methodInterceptorDemo();
//        invocationHandlerDemo();
//        immutableBeanDemo();
//    	beanGeneratorDemo();
    	
    	String xx="[{ 'busUserCode': '18112200005', 'busUserName': '高五', 'userType': 1, 'isEnabled': 0, 'lastLoginTime' : '2019-03-29 00:00:00'}]";
    	JSONArray.parseArray(xx);
    	
    }
    
    private static void methodFilterDemo() {
    	Enhancer enhancer = new Enhancer();
        CallbackHelper callbackHelper = new CallbackHelper(SampleClass.class, new Class[0]) {

			@Override
			protected Object getCallback(java.lang.reflect.Method method) {
				//过滤
				 if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class){
	                    return new FixedValue() {
	                        @Override
	                        public Object loadObject() throws Exception {
	                            return "Hello cglib";
	                        }
	                    };
	                }else{
	                    return NoOp.INSTANCE;
	                }
			}
 
        };
        enhancer.setSuperclass(SampleClass.class);
        enhancer.setCallbackFilter(callbackHelper);
        enhancer.setCallbacks(callbackHelper.getCallbacks());
        SampleClass proxy = (SampleClass) enhancer.create();
        proxy.test("nic");
        System.out.println(proxy.toString());
        System.out.println(proxy.hashCode());
    }

    private static void invocationHandlerDemo() {
    	Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(SampleClass.class);
        enhancer.setCallback(new InvocationHandler() {

			@Override
			public Object invoke(Object arg0, java.lang.reflect.Method method, Object[] arg2) throws Throwable {
//				 if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class){
						String k="hello cglib --"+ArrayUtil.join(arg2);
	                    return k;
//	                }else{
//	                    throw new RuntimeException("Do not know what to do");
//	                }
			}
			
			 
           
        });
        SampleClass proxy = (SampleClass) enhancer.create();
        proxy.test("nic");
        System.out.println(proxy.toString());
    }
    
    
	private static void methodInterceptorDemo() {
		Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(SampleClass.class);
        enhancer.setCallback(new MethodInterceptor() {

			@Override
			public Object intercept(Object obj, java.lang.reflect.Method m, Object[] args, MethodProxy proxy)
					throws Throwable {
				  	System.out.println("before method run...");
	                Object result = proxy.invokeSuper(obj, args);
	                System.out.println("after method run...");
	                return result;
			}
        });
        SampleClass sample = (SampleClass) enhancer.create();
        sample.test("go");
	}
	
	
	private static void immutableBeanDemo() {
		
		SampleBean bean = new SampleBean();
	    bean.setValue("Hello world");
	    
	    SampleBean immutableBean = (SampleBean) ImmutableBean.create(bean); //创建不可变类
	    
	    System.out.println(immutableBean.getValue());
	    bean.setValue("Hello world, again"); //可以通过底层对象来进行修改
	    System.out.println(immutableBean.getValue());
	    immutableBean.setValue("Hello cglib"); //直接修改将throw exception
		
	}
	
	public static void beanGeneratorDemo() throws Exception {
	    BeanGenerator beanGenerator = new BeanGenerator();
	    beanGenerator.setSuperclass(Entity.class);
	   
	    beanGenerator.addProperty("value",String.class);
	    
	    Object myBean = beanGenerator.create();
	    Method setter = myBean.getClass().getMethod("setValue",String.class);
	    setter.invoke(myBean,"Hello cglib");

	    Method getter = myBean.getClass().getMethod("getValue");
	    System.out.println(getter.invoke(myBean));
	}
	
}
